// pages/patient/detail.js
const api = require("./../../config/api")
const request = require("./../../utils/request")
const app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		patientInfo: '',
		isDefault: ''
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		console.log(options.isBindOne)
		this.getPatientDetial(options.id)
		this.setData({
			isDefault: options.isDefault,
			id:options.id,
			isBindOne:options.isBindOne
		})
	},
	getPatientDetial: async function(id) {
		const res = await request.get(api.patientDetail + '/' + id)
		if (res.httpStatus === 200) {
			switch (res.data.contactRelationType) {
				case 1:
					res.data.contactRelationType = '配偶'
					break;
				case 2:
					res.data.contacRelationType = '子'
					break;
				case 3:
					res.data.contactRelationType = '女'
					break;
				case 4:
					res.data.contactRelationType = '孙子、孙女、或外孙子、外孙女'
					break;
				case 5:
					res.data.contactRelationType = '父母'
					break;
				case 6:
					res.data.contactRelationType = '祖父母'
					break;
				case 7:
					res.data.contactRelationType = '外祖父母'
					break;
				case 8:
					res.data.contactRelationType = '兄、弟、姐、妹'
					break;
				case 9:
					res.data.contactRelationType = res.data.relation
					break;
				default:
			}
			res.data.birthday = res.data.birthday.substring(0,10)
			this.setData({
				patientInfo: res.data
			})
		}
	},
	// 设置默认患者
	setDefault: async function() {
		const userId = wx.getStorageSync('userId')
		const data = {
			patientId: this.data.patientInfo.id,
			userId: userId
		}
		const res = await request.post(api.setDefault, data)
		if (res.httpStatus === 200) {
			console.log(res)
			this.setData({
				isDefault: '1'
			})
			app.getPatientList()
			wx.showToast({
				title: '设置成功'
			})
		}
	},
	// 取消绑定
	unBind() {
		const that = this
		wx.showModal({
			content: '与患者解除绑定后将不能查看该患者的信息？',
			cancelText: "取消", //默认是“取消”
			confirmText: "确认", //默认是“确定”
			success: function(res) {
				if (res.cancel) {
					//点击取消,默认隐藏弹框
				} else {
					//点击确定
					that.unBindPatient()
				}
			},
			fail: function(res) {}, //接口调用失败的回调函数
			complete: function(res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）

		})
	},
	unBindPatient: async function() {
		const userId = wx.getStorageSync('userId')
		const data = {
			patientId: this.data.patientInfo.id,
			userId: userId
		}
		const res = await request.post(api.unbind, data)
		if (res.httpStatus === 200) {
			app.getPatientList()
			setTimeout(function() {
				wx.navigateBack({
					delta: 1
				})
			}, 1000)

		}
	},
	// 编辑患者
	edit() {
		const id = {
			id:this.data.patientInfo.id
		}
		const patientInfo = JSON.stringify(id)
		wx.navigateTo({
			url: '../createPatient/index?patientInfo=' + patientInfo
		})

	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
